$(function () {
    var questionId = $("#questionId").val();
    var isFree = $("#question-isFree").val();

    //初始化编辑框
    initSNU({
        id:'replyContext',
        type:1,
        toolsArr:null,
        height:300,
        gs:true,
        video:false
    });

    getQaReplyList(1);
    if(isFree == 2){
        getQaAttendList(1);
    }

    $('body').on('click','#upPage, #nextPage',function () {//评论列表上一页、下一页
        var pageNo = $(this).data('page');
        getQaReplyList(pageNo);
    }).on('click','#upPage2,#nextPage2',function () {
        var pageNo = $(this).data('page');
        getQaAttendList(pageNo);
    }).on('click','#save-reply', function () {//保存问题答案，不完结
        saveQuestionReply(false);
    }).on('click','#save-reply-complete', function () {//保存问题答案并完结
        icModale(2,'确定要发布该问题吗？问题回答后无法进行修改。',3,function () {
            $("#completeStatus").val(2);
            saveQuestionReply(true);
        });
    }).on('click','.save-questionAdd-addAnswer',function () {
        var obj = $(this);
        saveQuestionAdd(obj);
    }).on("click",".feed-photo",function(){
        var  href = $(this).attr('src');
        parent.layer.open({
            type: 1,
            area: winArea,
            skin: 'layui-layer-demo',
            title: '查看图片',
            content: '<div class="text-center"><img  src='+href+'></div>',
            btn: '确定',
            yes: function (index, layero) {
                parent.layer.close(index);

            }
        });
    }).on("click",".allReply",function(){
        $(this).addClass("reply").removeClass("allReply").text("收起");
        getAllReply($(this).data("id"),10);
    }).on("click",".reply",function() {
        $(this).addClass("allReply").removeClass("reply").text("查看全部");
        getAllReply($(this).data("id"), 3);
    });


    function getAllReply(id,pageSize) {
        $.ajax({
            url: baseloaction + '/admin/qa/reply/ajaxQueryQaReplyAllList',
            data: {'parentId': id,"pageSize":pageSize},
            dataType: 'json',
            type: 'post',
            success: function (response) {
                var left =$(".reply_"+id);
                $.each(left,function (index,date) {
                    $(date).remove();
                });
                var leafList =  response.list;
                if(leafList.length>0){
                    var html ="";
                    if(leafList.length>0){

                        $.each(leafList,function (index,date) {

                            html +=  '    <div class="media reply_'+id+'">' +
                                '                        <div class="media-left">' +
                                '                            <a href="#">' +
                                '                                <img class="media-object lazy"   style="width: 34px; height: 34px;" data-original="'+date.userMap.avatar+'" data-holder-rendered="true">' +
                                '                            </a>' +
                                '                        </div>' +
                                '                        <div class="media-body">';
                            html+=    '                            <span class="c-master">'+date.userMap.nickname+'</span>';

                            if(date.toUserMap!=null&&date.userId!=date.toUserId){
                                html+=    '<span class="c-master">@</span>' +
                                    '           <span class="c-master">'+date.toUserMap.nickname+'</span>';
                            }
                            html+= '                            <small class="fs12 ml10 c-999">'+date.createTime.substring(0,16)+'</small>' +
                                '                            <div>' +
                                '                                <p id="content_'+date.id+'">' + date.replyContext+'</p>' +
                                '                            </div>' +
                                '                        </div>' +
                                '                    </div>';

                        });
                        $("#reply_"+id).append(html);
                        layImg();
                    }




                }
            },
            error: function () {
                icModale(0, '系统繁忙，请稍候再试');
            }
        });
    }

    /**
     * 查询问题评论列表
     * @param pageNo
     */
    function getQaReplyList(pageNo) {
        var params = {"questionId":questionId};
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/qa/reply/ajaxQueryQaReplyList',//请求URLt
            contextId: 'reply-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: getQaReplyList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'all-qaTag', //全选框ID
            callback: function (pageNo) {//数据加载完成回调

            }
        };
        ajaxGetListData(obj);
        layImg();
    }

    /**
     * 查询旁听学员列表
     * @param pageNo
     */
    function getQaAttendList(pageNo) {
        var params = {"questionId":questionId};
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/qa/attend/ajaxQueryQaAttendList',//请求URLt
            contextId: 'attend-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: getQaAttendList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'all-qaTag', //全选框ID
            callback: function (pageNo) {//数据加载完成回调

            }
        };
        ajaxGetListData(obj);
    }

    /**
     * 保存问题答案
     * @param flag 是否完结问题：true：是，false：否
     */
    var _continue = true;
    function saveQuestionReply(flag) {
        if(_continue){
            _continue = false;
            var params = $("#replyForm").serializeJSON();
            params = JSON.stringify(params);
            $.ajax({
                url: baseloaction + '/admin/qa/question/saveQuestionReply',
                data: params,
                contentType:'application/json;charset=utf-8',
                type: 'post',
                dataType: 'json',
                success:function (result) {
                    icModale(0, result.message);
                    if(result.success){
                        $('#question_replyContext').html($('#replyContext').summernote('code'));
                        if(flag){
                            var _parent =  $("#btn-div").parents('form').parent('div.float-e-margins');
                            _parent.next().remove();
                            _parent.remove();
                        }
                    }
                    _continue = true;
                },
                error: function (error) {
                    icModale(0,"系统繁忙，请稍后重试");
                    _continue = true;
                }
            })
        }
    }

    /**
     * 保存追答内容
     * @param obj
     */
    function saveQuestionAdd(obj) {
        var _form = obj.parents('form');
        var addAnswer = _form.find('textarea').val();
        if(addAnswer.trim().length==0){
            icModale(0,'追答内容不能为空');
            return;
        }
        var params = _form.serializeJSON();
        params = JSON.stringify(params);
        $.ajax({
            url: baseloaction + '/admin/qa/add/saveQuestionAddAnswer',
            data: params,
            type: 'post',
            dataType: 'json',
            contentType: 'application/json;charset=utf-8',
            success: function (result) {
                icModale(0,result.message);
                if(result.success){
                    obj.parent().remove();
                    _form.find('textarea').attr("disabled","disabled");
                }
            },
            error:function (error) {
                icModale(0,'系统繁忙，请稍后重试');
            }
        })

    }

    function layImg(){
        $('img.lazy').lazyload({
            placeholder: staticPath + '/front/web/v3/img/default/default-face.gif',  //占位图
            effect: 'fadeIn' ,//载入方式
            threshold: 200, // 提前开始加载
        });
    }
});